package com.ruoyi.jksd.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.jksd.common.dto.XyUserBalanceDto;
import com.ruoyi.jksd.common.dto.XyUsersPwdDto;
import com.ruoyi.jksd.common.dto.XyuserDZDto;
import com.ruoyi.jksd.domain.XyUsers;
import com.ruoyi.jksd.mapper.XyUsersMapper;
import com.ruoyi.jksd.service.IXyUsersService;
import com.ruoyi.system.service.ISysUserService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 会员-用户Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-26
 */
@Service
public class XyUsersServiceImpl implements IXyUsersService {
    @Autowired
    private XyUsersMapper xyUsersMapper;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询会员-用户
     *
     * @param id 会员-用户主键
     * @return 会员-用户
     */
    @Override
    public XyUsers selectXyUsersById(String id) {
        return xyUsersMapper.selectXyUsersById(id);
    }

    @Override
    public XyUsers selectXyUsersByUserName(String userName) {
        return xyUsersMapper.selectXyUsersByUserName(userName);
    }

    /**
     * 查询会员-用户列表
     *
     * @param xyUsers 会员-用户
     * @return 会员-用户
     */
    @Override
    public List<XyUsers> selectXyUsersList(XyUsers xyUsers) {
        return xyUsersMapper.selectXyUsersList(xyUsers);
    }

    /**
     * 新增会员-用户
     *
     * @param xyUsers 会员-用户
     * @return 结果
     */
    @Override
    public int insertXyUsers(XyUsers xyUsers) {
        return xyUsersMapper.insertXyUsers(xyUsers);
    }

    /**
     * 修改会员-用户
     *
     * @param xyUsers 会员-用户
     * @return 结果
     */
    @Override
    public int updateXyUsers(XyUsers xyUsers) {
        return xyUsersMapper.updateXyUsers(xyUsers);
    }

    /**
     * 批量删除会员-用户
     *
     * @param ids 需要删除的会员-用户主键
     * @return 结果
     */
    @Override
    public int deleteXyUsersByIds(String[] ids) {
        return xyUsersMapper.deleteXyUsersByIds(ids);
    }

    /**
     * 删除会员-用户信息
     *
     * @param id 会员-用户主键
     * @return 结果
     */
    @Override
    public int deleteXyUsersById(String id) {
        return xyUsersMapper.deleteXyUsersById(id);
    }

    @Override
    public String buildInviteCode() {
        char arr[] = new char[6];
        int i = 0;
        while (i < 6) {
            char ch = (char) (int) (Math.random() * 124);
            if (ch >= 'A' && ch <= 'Z' || ch >= 'a' && ch <= 'z' || ch >= '0' && ch <= '9') {
                arr[i++] = ch;
            }
        }
        //将数组转为字符串
        String inveteCode = new String(arr);
        XyUsers xyUsers = new XyUsers();
        xyUsers.setInviteCode(inveteCode);
        List<XyUsers> tUsers = xyUsersMapper.selectXyUsersList(xyUsers);
        if (CollectionUtils.isNotEmpty(tUsers)) {
            buildInviteCode();
        }
        return inveteCode.toUpperCase();
    }

    @Override
    public int updatePassWord(XyUsersPwdDto xyUsers) {
        String newPwd = xyUsers.getNewPassword();
        XyUsers dbXyusers = xyUsersMapper.selectXyUsersById(xyUsers.getUid());
        String encryptNewPwd = SecurityUtils.encryptPassword(newPwd);
        if (newPwd.equals(dbXyusers.getPwd())) {
            dbXyusers.setPwd(newPwd);
            SysUser sysUser = sysUserService.selectUserByUserName(dbXyusers.getUsername());
            sysUser.setPassword(encryptNewPwd);
            sysUserService.updateUser(sysUser);
            return 1;
        } else {
            return 0;
        }

    }

    @Override
    public AjaxResult updateBalance(XyUserBalanceDto xyUserBalanceDto) {
        XyUsers xyUsers = xyUsersMapper.selectXyUsersById(xyUserBalanceDto.getUid());
        if (1 == xyUserBalanceDto.getDirect()) {
            xyUsers.setBalance(xyUsers.getBalance().add(xyUserBalanceDto.getAmount()));
        } else if (2 == xyUserBalanceDto.getDirect()) {
            if (xyUsers.getBalance().compareTo(xyUserBalanceDto.getAmount()) < 0) {
                return AjaxResult.error("yhyebz");
            }
            xyUsers.setBalance(xyUsers.getBalance().subtract(xyUserBalanceDto.getAmount()));
        } else {
            return AjaxResult.error("direct error");
        }
        xyUsersMapper.updateXyUsers(xyUsers);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult dzkd(XyuserDZDto xyuserDZDto) {
        XyUsers xyUsers = xyUsersMapper.selectXyUsersById(xyuserDZDto.getUid());
        xyUsers.setDz(xyuserDZDto.getDz());
        xyUsers.setDzc(xyuserDZDto.getDzc());
        xyUsers.setDzy(xyuserDZDto.getDzy());
        xyUsersMapper.updateXyUsers(xyUsers);
        return AjaxResult.success();
    }

}
